const Webpack = require("webpack");
const { merge } = require("webpack-merge");
const webpackBaseConfig = require("./webpack.base");
const UglifyJSWebpackPlugin = require("uglifyjs-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const OptimizeCSSAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin");

module.exports = merge(webpackBaseConfig, {
    mode: "production",
    plugins: [
        new Webpack.DefinePlugin({
            ENV: JSON.stringify('PROD'),
        }),
        new BundleAnalyzerPlugin(),
    ],
    optimization: {
        minimizer: [
            new UglifyJSWebpackPlugin({
                cache: true,
                parallel: true,
                sourceMap: true,
            }),
            new OptimizeCSSAssetsWebpackPlugin(),
        ],
        // splitChunks: {
        //     cacheGroups: {
        //         common: { // 处理非三方模块
        //             chunks: "all", // 【initial(代表对于直接引入的模块采取该优化策略)、async(代表对按需引入的模块采取该优化策略)、all(不管是直接引入还是按需引入都采取该优化策略)】
        //             minSize: 0, // 公共部分超过 0 字节就执行
        //             minChunks: 2, // 公共部分被引用超过 2 次就执行
        //         },
        //         vendor: {
        //             test: /node_modules/, // 指定匹配的目录
        //             chunks: "all",
        //             minSize: 0,
        //             minChunks: 2,
        //         },
        //     },
        // },
    },
});